package person.bing.fbcloud.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import person.bing.fbcloud.api.dto.ForumUserDTO;
import person.bing.fbcloud.api.entity.ForumUser;
import person.bing.fbcloud.common.core.exception.CheckedException;
import person.bing.fbcloud.common.core.util.R;
import person.bing.fbcloud.service.ForumUserService;

/**
 * 用户
 * @author mengfb
 * @since 2019-06-24
 */
@SuppressWarnings({"ALL", "AlibabaClassMustHaveAuthor"})
@RestController
@RequestMapping("/forumuser")
public class ForumUserController {
    @Autowired
    private ForumUserService forumUserService;

    /**
     * 注册
     * @param forumUserDTO
     * @return
     */
    @PostMapping
    public R signUp(@RequestBody ForumUserDTO forumUserDTO){
        String userCode = forumUserDTO.getUserCode();
        String userPwd = forumUserDTO.getUserPwd();
        if (StrUtil.isEmpty(userCode)){
            throw new CheckedException("userCode不能为空");
        }
        if (StrUtil.isEmpty(userPwd)){
            throw new CheckedException("userPwd不能为空");
        }
        forumUserService.signUp(userCode,userPwd);
        return new R();
    }


    /**
     * 登录
     * @param forumUserDTO
     * @return
     */
    @GetMapping
    public R signIn(ForumUserDTO forumUserDTO){
        String userCode = forumUserDTO.getUserCode();
        String userPwd = forumUserDTO.getUserPwd();
        if (StrUtil.isEmpty(userCode)){
            throw new CheckedException("userCode不能为空");
        }
        if (StrUtil.isEmpty(userPwd)){
            throw new CheckedException("userPwd不能为空");
        }
        forumUserService.signIn(userCode,userPwd);
        return new R();
    }

    /**
     * 根据用户名获取用户
     * @param jsonObject
     * @return
     */
    @PostMapping("/getUserByUserName")
    public R getUserByUserName(@RequestBody JSONObject jsonObject){
        String userName = jsonObject.getStr("userName");
        if (StrUtil.isEmpty(userName)){
            throw new CheckedException("userName不能为空！！");
        }
        ForumUser forumUser = forumUserService.getOne(Wrappers.<ForumUser>query().lambda()
                .eq(ForumUser::getUserCode, userName)
        );
        if (ObjectUtil.isNotNull(forumUser)){
            forumUser.setCreateTime(null);
            forumUser.setUpdateTime(null);
        }
        return new R(forumUser);
    }

}
